"""Controller for changes menu."""

import cherrypy
from turbogears import expose, redirect
from turbogears.controllers import Controller
from mnemoply.controllers.crud import CRUDController, ItemTab
from mnemoply.controllers.lookup import TableCol, LookupController, TableLookupDataSource

from mnemoply import model

class ChildrenItemTab(ItemTab):
    title = _("Children")
    name = "children_tab"
    template = "../menu_children_tab"
    
    @expose()
    def up_child(self, obj, cid):
        child = model.Menu.get(int(cid))
        assert child.parent == obj
        min_ordinal = model.Menu.selectBy(parent=child.parent).min('ordinal')
        if child.ordinal > min_ordinal or \
        len(list(model.Menu.selectBy(parent=child.parent, ordinal=child.ordinal)))>1:
            i = 0
            prev = []
            while len(prev)==0 or ((prev[0].id == child.id) and len(prev)==1):
                prev = list(model.Menu.selectBy(parent=child.parent, ordinal=child.ordinal - i))
                i+=1
            else:
                if(prev[0].id!=child.id):
                    prev_child = prev[0]
                    ordinal = child.ordinal
                    prev_child.ordinal = ordinal
                    child.ordinal = ordinal - 1
                else:
                    prev_child = prev[1]
                    ordinal = child.ordinal
                    prev_child.ordinal = ordinal
                    child.ordinal = ordinal - 1
        raise redirect("./")
    
    @expose()
    def down_child(self, obj, cid):
        child = model.Menu.get(int(cid))
        assert child.parent == obj
        max_ordinal = model.Menu.selectBy(parent=child.parent).max('ordinal')
        if child.ordinal < max_ordinal or \
        len(list(model.Menu.selectBy(parent=child.parent, ordinal=child.ordinal)))>1:
            i = 0
            next = []
            while len(next)==0 or ((next[0].id == child.id) and len(next)==1):
                next = list(model.Menu.selectBy(parent=child.parent, ordinal=child.ordinal + i))
                i+=1
            else:
                if(next[0].id!=child.id):
                    next_child = next[0]
                    ordinal = child.ordinal
                    next_child.ordinal = ordinal
                    child.ordinal = ordinal + 1
                else:
                    next_child = next[1]
                    ordinal = child.ordinal
                    next_child.ordinal = ordinal
                    child.ordinal = ordinal + 1
    
        raise redirect("./")

class MenuController(LookupController,CRUDController):
    lookup_columns = [
        TableCol(name='id', caption=_("ID"), size=1),
        TableCol(name='name', caption=_("Name"), size=14),
        TableCol(name='href', caption=_("href"), size=14),
        TableCol(name='ordinal', caption=_("Ordinal"), size=14),
        TableCol(name='parent_id', caption=_("Parent"), size=14)
    ]
    item_tabs = [ChildrenItemTab()]
    edit_fields = add_fields = [
        "name",
        "href",
        "parentID"
    ]
    view_fields = edit_fields
    model_object = model.Menu

    def __init__(self, data_source=TableLookupDataSource('menu')):
        LookupController.__init__(self,
                                  data_source=data_source,
                                  columns=self.lookup_columns,
                                  mass_actions_class=self.model_object,
                                  title=self.model_object.model_title,)
        CRUDController.__init__(self, model_object=self.model_object)

    @expose()
    def change(self, menu_number=1):
        cherrypy.session.acquire_lock()
        cherrypy.session['menu_number'] = menu_number
        raise redirect("/")
    
    
exposed = MenuController()
